import storage from 'good-storage'

const _SELLER_ = 'seller'

// function saveToLocal(id, key, value) {
//   const userLocal = storage.get(_SELLER_, {})
//   if (!userLocal[id]) {
//     userLocal[id] = {}
//   }
//   userLocal[id][key] = value
//   storage.set(_SELLER_, userLocal)

// }

// function loadFromLocal(id, key, defaultValue) {
//   const userLocal = storage.get(_SELLER_, {})
//   if (!userLocal[id] || userLocal[id][key] === undefined) {
//     return defaultValue
//   } else {
//     return userLocal[id][key]
//   }
// }




function saveToLocal(id, key, value) {
  let userLocal = window.localStorage.getItem(_SELLER_)
  if (userLocal) {
    userLocal = JSON.parse(userLocal)
  } else {
    userLocal = {}
  }

  if (!userLocal[id]) {
    userLocal[id] = {}
  }

  userLocal[id][key] = value
  window.localStorage.setItem(_SELLER_, JSON.stringify(userLocal))
}

function loadFromLocal(id, key, defaultValue) {
  let userLocal = window.localStorage.getItem(_SELLER_)
  if (!userLocal) {
    return defaultValue
  } else {
    userLocal = JSON.parse(userLocal)
  }

  if (!userLocal[id] || userLocal[id][key] === undefined) {
    return defaultValue
  } else {
    return userLocal[id][key]
  }
}

export { saveToLocal, loadFromLocal }